You are here: TI-Nspire™ Technology Program Editor eGuide > TI-Nspire™ Technology Program Editor Guide > Getting Started with the Program Editor > Using Loops to Repeat a Group of Commands

Using Loops to Repeat a Group of Commands

To repeat the same group of commands successively, use one of the loop structures. Several types of loops are available. Each type gives you a different way to exit the loop, based on a conditional test.

Loop and loop-related commands reside on the Program Editor’s Control and Transfers menus.

When you insert one of the loop structures, its template is inserted at the cursor location. You can then begin entering the commands that will be executed within the loop.

For...EndFor Loops

A For...EndFor loop uses a counter to control the number of times the loop is repeated. The syntax of the For command is:

 

For

variable,
À

begin,
Á

end [,
Â

increment ]
Ã

À

Name of a variable to be used as a counter

Á

Value assigned to variable when the For loop begins.

Â

Value compared to the current value of variable at each iteration of the loop. The loop exits when variable exceeds end.

Ã

Value added to variable at each iteration of the loop (This argument is optional. The default increment is 1.)

At each iteration of the For loop, the variable value is compared to the end value. If variable does not exceed end, the commands within the For...EndFor loop are executed and the loop repeats; otherwise, control jumps to the command following EndFor.

Note: The For command automatically increments the counter variable so that the function or program can exit the loop after a certain number of repetitions.

At the end of the loop (EndFor), control loops back to the For command, where the counter variable is incremented and compared to end.

For example:

For i,0,5,1

  Disp i  À

EndFor

Disp i  Á

À

Displays 0, 1, 2, 3, 4, and 5.

Á

Displays 6. When variable increments to 6, the loop is not executed.

Notes:

You can declare variable as local if it does not need to be saved after the function or program stops.
You can set end to a value less than begin, provided you also set increment to a negative value.

While...EndWhile Loops

A While...EndWhile loop repeats a block of commands as long as a specified condition is true. The syntax of the While command is:

While condition

When While is executed, condition is evaluated. If condition is true, the loop is executed; otherwise, control jumps to the command following EndWhile.

Note: The While command does not automatically change the condition. You must include commands that allow the function or program to exit the loop.

At the end of the loop (EndWhile), control jumps back to the While command, where condition is re-evaluated.

To execute the loop the first time, the condition must initially be true.

Any variables referenced in the condition must be set before the While command. (You can build the values into the function or program, or you can prompt the user to enter the values.)
The loop must contain commands that change the values in the condition, eventually causing it to be false. Otherwise, the condition is always true and the function or program cannot exit the loop (called an infinite loop).

For example:

0&x  À

While x<5

  Disp x  Á

  x+1&x  Â

EndWhile

Disp x  Ã

À

Initially sets x.

Á

Displays 0, 1, 2, 3, and 4.

Â

Increments x.

Ã

Displays 5. When x increments to 5, the loop is not executed.

Loop...EndLoop Loops

A Loop...EndLoop creates an infinite loop, which is repeated endlessly. The Loop command does not have any arguments.

Typically, you insert commands in the loop that let the program exit from the loop. Commonly used commands are: If, Exit, Goto, and Lbl (label). For example:

0&x

Loop

  Disp x

  x+1&x

  If x>5  À

    Exit

EndLoop

Disp x  Á

À

An If command checks the condition.

Á

Exits the loop and jumps to here when x increments to 6.

Note: The Exit command exits from the current loop.

In this example, the If command can be anywhere in the loop.

When the If command is:

The loop is:

At the beginning of the loop

Executed only if the condition is true.

At the end of the loop

Executed at least once and repeated only if the condition is true.

The If command could also use a Goto command to transfer program control to a specified Lbl (label) command.

Repeating a Loop Immediately

The Cycle command immediately transfers program control to the next iteration of a loop (before the current iteration is complete). This command works with For...EndFor, While...EndWhile, and Loop...EndLoop.

Lbl and Goto Loops

Although the Lbl (label) and Goto commands are not strictly loop commands, they can be used to create an infinite loop. For example:

As with Loop...EndLoop, the loop should contain commands that let the function or program exit from the loop.